<?php 
/**
  * 管理员控制器
  * date 2013-1-27 
  */
class AdministratorAction extends CommonAction {

  private $allRoleArr=array();

	public function _initialize()
	{
    $this->currentModel = D(MODULE_NAME);
		parent::_initialize();
    $this->allRoleArr=M('Role')->where(array('status'=>1))->select();
    $this->assign('allRoleArr',$this->allRoleArr);
	}


  public function index()
  {
    $map=$this->_search();
    if($_SESSION['administrator']!=1)$map['is_super']=0;
    $this->_filter($map);
    $count=$this->currentModel->where($map)->count();
    $page=$this->_page($count,C('PAGE_SUM'));
    $datas=$this->currentModel->where($map)->limit($page->firstRow.','.$page->listRows)->order($this->pkId.' DESC')->select();
    $this->assign('page',$page->show());
    $this->assign('datas',$datas);    
    $this->display();
  }

  /**
    * 过滤搜索条件
    * @access private
    * @param array &$map
    */
  private function _filter(&$map)
  {
    $this->assign('param',$map);
    if(isset($map['username']))
    {
          $map['username']=array('like',"%".$map['username']."%");
    }       
  }  

  public function add()
  {
    if($this->isPost())
    {
      $this->_save();
    }else
        {
          $this->display();
        }
  }

  public function edit()
  {
    if($this->isPost())
    {
      $this->_save();
    }else
        {
          $id=$this->_get($this->pkId);
          $data=$this->currentModel->find($id);
          if(empty($data))$this->error('非法操作',U(MODULE_NAME.'/index'));
          $this->assign('data',$data);
          $this->display('add');
        }
  }

/**
 * 修改密码
 * @access public 
 */
  public function password()
  {
    if($this->isPost())
    {
      $id=$this->_post($this->pkId);
      $data=$this->currentModel->find($id);
      if($this->_post('passwordAgain')!=$this->_post('password'))
      {
          $this->error('两次输入的密码不一致',U(MODULE_NAME.'/password',array('id'=>$data['id'])));
      }else
          {
              $data['id']=$id;
              $data['password']=md5($this->_post('password'));
              if($this->currentModel->data($data)->save()!==false)
              {
                $this->success('成功',U(MODULE_NAME.'/index'));
              }else
                  {
                    $this->error('失败');
                  }
          }
    }else
        {
          $id=$this->_get($this->pkId);
          if($id=='')$id=$_SESSION[C('USER_AUTH_KEY')];
          $data=$this->currentModel->find($id);
          if(empty($data))$this->error('非法操作',U(MODULE_NAME.'/index'));
          $this->assign('data',$data);
          $this->display();          
        }
  }

  /**
  * 删除数据
  * @access public
  */
  public function del()
  {
    $id=$this->_request($this->pkId);
    if(is_array($id))$id=implode(',',$id);
    if($this->currentModel->where()->delete($id)>0)
    {
      $this->success('成功',U(MODULE_NAME.'/index'));
    }else
      {
        $this->error('失败',U(MODULE_NAME.'/index'));
      }
  }  

  /**
  * 改变信息状态
  * @access public
  */
  public function change()
  {
    $id=$this->_post($this->pkId);
    if(empty($id))$this->error('请选择操作信息',U(MODULE_NAME.'/index'));
    $funcName=$this->_get('type');
    if(method_exists($this->currentModel,$funcName))
    {
      foreach($id as $v)
      {
        $this->currentModel->$funcName(array('id'=>$v));
      }
    }else{$this->error('非法操作',U(MODULE_NAME.'/index'));}
    $this->success('成功',U(MODULE_NAME.'/index'));
  }  

  private function _save()
  {
      if($this->currentModel->create()===FALSE)
      {
        $this->error($this->currentModel->getError());
      }
      $id=$this->_post($this->pkId);
      $funcName=$id>0?'save':'add';
      if($this->currentModel->$funcName()!==FALSE)
      {
        $this->success('成功',U(MODULE_NAME.'/index'));
      }else
          {
            $this->error('失败');
          }      
  }  

}	
 ?>